package com.amt.olcacon;

import io.minio.GetObjectArgs;
import io.minio.MinioClient;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.InputStream;

public class ExcelFromMinio {
    public static void main(String[] args) {
        try {
            // 1. 初始化 MinIO 客户端
            MinioClient minioClient = MinioClient.builder()
                    .endpoint("http://119.45.166.189:9000")
                    .credentials("6ZeW1mQz0pXQlWkYndLk", "oIkc9yA8SM2xnj0tUBdLVF98XutSufcRcHBliawY")
                    .build();

            // 2.获取地址
            String url = RegexUrlConverter.convertUrlToFilename("/upload/工序导入模板v2.0.xlsx?fileName=工序导入模板v2.0.xlsx&fut=1757397452561&ai=5cee4b16-a895-4d29-a002-c2845cf526ec&con=lcap_default_connection&fpp=/official_website");

            // 3. 从 MinIO 获取 Excel 文件流
            try (InputStream stream = minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket("codewave")
                            .object("official_website/工序导入模板v2.0(1)_fut1757387586758_ai5cee4b16-a895-4d29-a002-c2845cf526ec_conlcap_default_connection.xlsx")
                            .build());
                 Workbook workbook = new XSSFWorkbook(stream)) {

                // 3. 读取第一个 sheet
                Sheet sheet = workbook.getSheetAt(0);
                System.out.println("读取到的 Sheet 名称: " + sheet.getSheetName());

                // 4. 简单打印前几行内容
                for (int i = 0; i <= sheet.getLastRowNum(); i++) {
                    Row row = sheet.getRow(i);
                    if (row == null) continue;
                    for (int j = 0; j < row.getLastCellNum(); j++) {
                        Cell cell = row.getCell(j);
                        if (cell == null) continue;
                        cell.setCellType(CellType.STRING);
                        System.out.print(cell.getStringCellValue() + "\t");
                    }
                    System.out.println();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}